package com.moody.hibernate.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.ClassifyRecord;
import com.moody.hibernate.domain.DebtCollateral;

/**
 	* A data access object (DAO) providing persistence and search support for ClassifyRecord entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see com.moody.hibernate.domain.ClassifyRecord
  * @author MyEclipse Persistence Tools 
 */

public class ClassifyRecordDAO extends HibernateDaoSupport  {
	     private static final Logger log = LoggerFactory.getLogger(ClassifyRecordDAO.class);
		//property constants
	public static final String DEBT_NO = "debtNo";
	public static final String FIRST_RESULT = "firstResult";
	public static final String CLASSIFY_DESC = "classifyDesc";
	public static final String FRUSER_ID = "fruserId";
	public static final String FRORG_ID = "frorgId";
	public static final String FIRST_DATE = "firstDate";
	public static final String STATUS = "status";
	public static final String CONFIRM_RESULT = "confirmResult";
	public static final String CONFIRM_DESC = "confirmDesc";
	public static final String CONFIRM_USER_ID = "confirmUserId";
	public static final String CONFIRM_ORG_ID = "confirmOrgId";
	public static final String CONFIRM_DATE = "confirmDate";



	protected void initDao() {
		//do nothing
	}
    
    public void save(ClassifyRecord transientInstance) {
        log.debug("saving ClassifyRecord instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(ClassifyRecord persistentInstance) {
        log.debug("deleting ClassifyRecord instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public ClassifyRecord findById( java.lang.String id) {
        log.debug("getting ClassifyRecord instance with id: " + id);
        try {
            ClassifyRecord instance = (ClassifyRecord) getHibernateTemplate()
                    .get("com.moody.hibernate.domain.ClassifyRecord", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(ClassifyRecord instance) {
        log.debug("finding ClassifyRecord instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding ClassifyRecord instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from ClassifyRecord as model where model." 
         						+ propertyName + "= ?";
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByDebtNo(Object debtNo
	) {
		return findByProperty(DEBT_NO, debtNo
		);
	}
	
	public List findByFirstResult(Object firstResult
	) {
		return findByProperty(FIRST_RESULT, firstResult
		);
	}
	
	public List findByClassifyDesc(Object classifyDesc
	) {
		return findByProperty(CLASSIFY_DESC, classifyDesc
		);
	}
	
	public List findByFruserId(Object fruserId
	) {
		return findByProperty(FRUSER_ID, fruserId
		);
	}
	
	public List findByFrorgId(Object frorgId
	) {
		return findByProperty(FRORG_ID, frorgId
		);
	}
	
	public List findByFirstDate(Object firstDate
	) {
		return findByProperty(FIRST_DATE, firstDate
		);
	}
	
	public List findByStatus(Object status
	) {
		return findByProperty(STATUS, status
		);
	}
	
	public List findByConfirmResult(Object confirmResult
	) {
		return findByProperty(CONFIRM_RESULT, confirmResult
		);
	}
	
	public List findByConfirmDesc(Object confirmDesc
	) {
		return findByProperty(CONFIRM_DESC, confirmDesc
		);
	}
	
	public List findByConfirmUserId(Object confirmUserId
	) {
		return findByProperty(CONFIRM_USER_ID, confirmUserId
		);
	}
	
	public List findByConfirmOrgId(Object confirmOrgId
	) {
		return findByProperty(CONFIRM_ORG_ID, confirmOrgId
		);
	}
	
	public List findByConfirmDate(Object confirmDate
	) {
		return findByProperty(CONFIRM_DATE, confirmDate
		);
	}
	

	public List findAll() {
		log.debug("finding all ClassifyRecord instances");
		try {
			String queryString = "from ClassifyRecord";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public ClassifyRecord merge(ClassifyRecord detachedInstance) {
        log.debug("merging ClassifyRecord instance");
        try {
            ClassifyRecord result = (ClassifyRecord) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(ClassifyRecord instance) {
        log.debug("attaching dirty ClassifyRecord instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(ClassifyRecord instance) {
        log.debug("attaching clean ClassifyRecord instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static ClassifyRecordDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (ClassifyRecordDAO) ctx.getBean("ClassifyRecordDAO");
	}

	 public void update(ClassifyRecord transientInstance) {
        log.debug("updating ClassifyRecord instance");
        try {
            getHibernateTemplate().update(transientInstance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }
	 
	 @SuppressWarnings("unchecked")
	public List<ClassifyRecord> findByDebtNo(String debtNo) {
		 
		 List<ClassifyRecord> list = new ArrayList<ClassifyRecord>();
		 try{
			 String queryString = "from ClassifyRecord as cr where cr.debtNo = ? order by status asc, cr.confirmDate desc";
			 list =  getHibernateTemplate().find(queryString, debtNo);
			 }catch (Exception e){
				 log.error("query error", e);
			}
			 return list;
	 }
	 
}